/**
 * 
 */
package org.insia.smartposse.xfire.service;

import org.codehaus.xfire.MessageContext;
import org.insia.smartposse.config.DAOFactory;
import org.insia.smartposse.metier.Company;
import org.insia.smartposse.metier.Freelance;
import org.insia.smartposse.metier.User;
import org.insia.smartposse.metier.webservice.LightCompany;
import org.insia.smartposse.metier.webservice.LightFreelance;
import org.insia.smartposse.metier.webservice.LightUser;
import org.insia.smartposse.xfire.iservice.IAuthenticationService;

/**
 * @author raguerri
 * @since 19 avr. 07
 * @version 0.0.1
 * 
 */
public class AuthenticationService implements IAuthenticationService {

	/* (non-Javadoc)
	 * @see org.insia.smartposse.xfire.iservice.IAuthenticationService#authenticate(java.lang.String, java.lang.String)
	 */
	public LightUser authenticate(String login, String password,final MessageContext context) {
		LightUser result = null;
		User u = DAOFactory.getUserDAO().getUserByPasswordLogin(login, password);
		if (u!=null) {
			context.getSession().put("user", u);
			if (u instanceof Freelance) {
				result = (LightFreelance)((Freelance)u).setLight();
			} else if(u instanceof Company) {
				result = (LightCompany)((Company)u).setLight();
			} else {
				result = (LightUser)u.setLight();
			}
		}
		return result;
	}

}
